package com.ruohi.manage;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import com.ruohi.util.common.Common;
import com.ruohi.util.common.CommonDate;
import com.ruohi.util.common.CommonString;
import com.ruohi.util.dbutil.DBHelper;

public class JobExecute implements Job {

	@SuppressWarnings("unchecked")
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		DBHelper dbHelper = DBHelper.getInstance();
		Connection connection = null;
		try {
			connection = dbHelper.getConnection();
			JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
			List<Map> list = (List<Map>) jobDataMap.get("schedulers");
			if (null != list) {
				int size = list.size();
				if (size > 0) {
					String http_url = "";
					Object[] params = new Object[8];
					Map m = null;
					String sql = " insert into service_common_log ( add_user , add_date , add_ip , uuid , channel_name , content , type , log_level ) ";
					sql += " values ( ? , ? , ? , ? , ? , ? , ? , ? ) ";
					for (int i = 0; i < size; i++) {
						m = list.get(i);
						try {
							http_url = CommonString.getFormatPara(m.get("http_url"));
							Common.doGetHttpAsync(http_url);
							params[0] = "job";
							params[1] = CommonDate.getToday("yyyy-MM-dd HH:mm:ss");
							params[2] = "";
							params[3] = CommonString.getUUID();
							params[4] = "Service项目调度执行";
							params[5] = "成功--访问了" + http_url + "，调度类型为：" + jobDataMap.getString("name");
							params[6] = "105001";
							params[7] = 0;
							dbHelper.execute(sql, params, connection);
						} catch (Exception e) {
							e.printStackTrace();
							params[0] = "job";
							params[1] = CommonDate.getToday("yyyy-MM-dd HH:mm:ss");
							params[2] = "";
							params[3] = CommonString.getUUID();
							params[4] = "Service项目调度执行";
							params[5] = "失败--访问了" + http_url + "，调度类型为：" + jobDataMap.getString("name");
							params[6] = "105001";
							params[7] = 0;
							dbHelper.execute(sql, params, connection);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.freeConnection(connection);
		}
	}

	public void shutdown() throws Exception {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		scheduler.shutdown();
	}

	@SuppressWarnings("unchecked")
	public void start() throws Exception {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		DBHelper dbHelper = DBHelper.getInstance();
		Connection connection = null;
		try {
			connection = PoolManager.getConnection(DBType.SERVICE);
			String sql = " select no,name,val ";
			sql += " from service_common_enum ";
			sql += " where parent_no = '104' and is_show = 1 ";
			// 调度类
			JobDetail jobDetail = null;
			JobDataMap jobDataMap = null;
			CronTrigger trigger = null;
			// 查询出所有的调度类型
			List<Map> types = dbHelper.getMapList(sql, connection);
			sql = " select http_url from service_schedule where schedule_type = ? and is_use = 1 ";
			if (null != types && types.size() > 0) {
				List<Map> schedulers = null;
				String no = "";
				for (Map t : types) {
					no = CommonString.getFormatPara(t.get("no"));
					schedulers = dbHelper.getMapList(sql, new Object[] { no }, connection);
					jobDetail = new JobDetail(no + "_job", no + "_group", JobExecute.class);
					jobDataMap = new JobDataMap();
					jobDataMap.put("schedulers", schedulers);
					jobDataMap.put("name", t.get("name"));
					jobDetail.setJobDataMap(jobDataMap);
					trigger = new CronTrigger(no + "_triger", no, CommonString.getFormatPara(t.get("val")));
					scheduler.scheduleJob(jobDetail, trigger);
					scheduler.start();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.freeConnection(connection);
		}
	}

}
